package com.print;

import com.render.MyCellRenderer;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.text.MessageFormat;
import java.util.Enumeration;

public class Reports {

    JTable table;
    JLabel reportNameLabel;
    private Enumeration<TableColumn> tcol;
    private int columnID;

    public Reports(Object data[][], String col[], final String reportName) {
        try {
            UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        } catch (Exception e) {
        }

        final JFrame frame = new JFrame(" تقرير! ");
        JPanel panel = new JPanel();
        DefaultTableModel model = new DefaultTableModel(data, col);
        table = new JTable(model);
        table.setRowHeight(27);
        tcol = table.getColumnModel().getColumns();
        while (tcol.hasMoreElements()) {
            tcol.nextElement().setCellRenderer(new MyCellRenderer());
        }
        // Resize column width AS it's Name data
        columnID = table.getColumnModel().getColumnIndex("مسلسل");
        table.getColumnModel().getColumn(columnID).setPreferredWidth(40);
        columnID = table.getColumnModel().getColumnIndex("الأسم");
        table.getColumnModel().getColumn(columnID).setPreferredWidth(300);
        columnID = table.getColumnModel().getColumnIndex("الفئة");
        table.getColumnModel().getColumn(columnID).setPreferredWidth(30);
        columnID = table.getColumnModel().getColumnIndex("المنطقة");
        table.getColumnModel().getColumn(columnID).setPreferredWidth(130);
        columnID = table.getColumnModel().getColumnIndex("الأفراد");
        table.getColumnModel().getColumn(columnID).setPreferredWidth(35);
        try {
            columnID = table.getColumnModel().getColumnIndex("الموزع");
            table.getColumnModel().getColumn(columnID).setPreferredWidth(130);
        } catch (Exception e) {
        }
        JTableHeader header = table.getTableHeader();
        header.setBackground(Color.yellow);
        JScrollPane pane = new JScrollPane(table);
        panel.add(pane);
        frame.add(panel);
        frame.setSize(550, 550);
        frame.setUndecorated(true);
        frame.getRootPane().setWindowDecorationStyle(JRootPane.PLAIN_DIALOG);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);

        Toolkit t = Toolkit.getDefaultToolkit();
        Dimension d = t.getScreenSize();
        int x = (d.width - frame.getWidth()) / 2;
        int y = (d.height - frame.getHeight()) / 2;
        frame.setLocation(x, y);

// Report Name
        reportNameLabel = new JLabel();
        reportNameLabel.setText(reportName);
        reportNameLabel.setHorizontalAlignment(JLabel.RIGHT);
        reportNameLabel.setFont(new java.awt.Font("Traditional Arabic", 0, 24));
        frame.getContentPane().add(
                BorderLayout.NORTH, reportNameLabel);

        JButton printButton = new JButton();
        printButton.setText("طباعة التقرير");
        frame.getContentPane().add(
                BorderLayout.SOUTH, printButton);
        printButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                try {
                    MessageFormat headerFormat = new MessageFormat(reportName);
                    MessageFormat footerFormat = new MessageFormat("- {0} -");
                    table.print(JTable.PrintMode.NORMAL, headerFormat, footerFormat, false, null, false);
                    frame.dispose();
                } catch (PrinterException pe) {
                    System.err.println("Error printing: " + pe.getMessage());
                }
            }
        });
    }
}
